import shopCategoryService,{ queryObject } from "../service/shopCategoryService";
import { ElMessage } from "element-plus";
import { useRouter,useRoute } from "vue-router";

export interface IRule{
    queryCategory:(obj:queryObject,callback:any)=>void,
    getList:(callback:any)=>void,
    AddCategory:(obj:queryObject,queryData:queryObject,callback:any)=>void,
    delCategory:(id:number,queryData:queryObject,callback:any)=>void,
    EditCategory:(obj:queryObject,queryData:queryObject,callback:any)=>void,
    getCategoryById:(id:number | string,callback:any)=>void,

}
export function CategoryHook(): IRule{
    const router=useRouter()
    console.log("router",router);

    //获取商品分类列表
    function getList(callback:any){
        shopCategoryService.getCategoryList().then((res:any)=>{
            if(res.data.code==200||res.data.code==0){
                console.log("请求信息",res.data)
                callback(res.data.data,res.data.count)
            } else{
                ElMessage.error(res.data.msg)
            }
        }).catch(()=>{
            ElMessage("获取分类信息失败")
        })
    }
    
    //模糊查询
    function queryCategory(obj:queryObject,callback:any){
        shopCategoryService.CategoryQuery(obj).then((res:any)=>{
            console.log("请求信息",res.data)
            if(res.data.code==200||res.data.code==0){
                console.log("查询信息",res.data);
                callback(res.data.data,res.data.count)
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch(()=>{
            ElMessage("请求失败拉！")
        })
    }
    //根据id获取
    function getCategoryById(id:number | string,callback:any){
        shopCategoryService.CategoryId(id).then((res:any)=>{
            if(res.data.code==200||res.data.code==0){
                console.log("请求信息",res.data)
                callback(res.data.data)
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch((err:any)=>{
            console.log("id获取",err);
            ElMessage("请求失败")
        })
    }
    //添加分类
    function AddCategory(obj:queryObject,queryData:queryObject,callback:any){
        shopCategoryService.CategoryAdd(obj).then((res:any)=>{
            console.log("请求信息",res.data)
            if(res.data.code==200||res.data.code==0){
                ElMessage({type:"success",message:res.data.msg})
                queryCategory(queryData,callback)
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch((err:any)=>{
            console.log("error",err);
            ElMessage("分类添加失败")
        })
    }
    //修改分类
    function EditCategory(obj:queryObject,queryData:queryObject,callback:any){
        shopCategoryService.CategoryUpdate(obj).then((res:any)=>{
            if(res.data.code==200||res.data.code==0){
                console.log("请求：",res.data);
                ElMessage({type:"success",message:res.data.msg})
                queryCategory(queryData,callback)
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch((err:any)=>{
            console.log("err:",err);
            ElMessage("修改哦豁")
        })
    }
    //删除
    function delCategory(id:number,queryData:queryObject,callback:any){
        console.log("id",id);
        shopCategoryService.categoryDelete(id).then((res:any)=>{
            console.log("删除信息",res.data)
            if(res.data.code==200||res.data.code==0){
                ElMessage({type:"success",message:res.data.msg})
                queryCategory(queryData,callback)
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch((err:any)=>{
            console.log("err:",err);
            ElMessage("删除失败！")
        })
    }

    return{
        getList,
        queryCategory,
        getCategoryById,
        AddCategory,
        EditCategory,
        delCategory
    }
}